/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastrarEmpregado1.java
 *
 * Created on 11/12/2011, 15:44:55
 */

package gui;

import DAO.DAOGenerico;
import DAO.DAOJSON;
import empregado.Empregado;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import utils.StringParaData;
import validators.Validator;
import validators.annotations.ValidateAnnotationParser;

/**
 *
 * @author thiago
 */
public class CadastrarEmpregado extends javax.swing.JDialog {

    private String cpf;
    private String nome;
    private String sexo;
    private GregorianCalendar dtNascimento;
    private GregorianCalendar dtAdmissao;
    private double salario;
    private GregorianCalendar dtDesligamento;

    /** Creates new form CadastrarEmpregado1 */
    public CadastrarEmpregado(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupsexo = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        labelnome = new javax.swing.JLabel();
        txtnome = new javax.swing.JTextField();
        labelcpf = new javax.swing.JLabel();
        txtcpf = new javax.swing.JTextField();
        labelsexo = new javax.swing.JLabel();
        botaomasculino = new javax.swing.JRadioButton();
        botaofeminino = new javax.swing.JRadioButton();
        labelnascimento = new javax.swing.JLabel();
        txtnascimento = new javax.swing.JTextField();
        labeladmissao = new javax.swing.JLabel();
        txtadmissao = new javax.swing.JTextField();
        labelsalario = new javax.swing.JLabel();
        txtsalario = new javax.swing.JTextField();
        labeldesligamento = new javax.swing.JLabel();
        txtdesligamento = new javax.swing.JTextField();
        botaocadastrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setForeground(new java.awt.Color(2, 22, 28));
        jLabel1.setText("Cadastrar Empregado");

        labelnome.setText("Nome");

        txtnome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtnomeActionPerformed(evt);
            }
        });
        txtnome.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtnomeFocusLost(evt);
            }
        });

        labelcpf.setText("CPF");

        txtcpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcpfActionPerformed(evt);
            }
        });
        txtcpf.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtcpfFocusLost(evt);
            }
        });

        labelsexo.setText("Sexo");

        buttonGroupsexo.add(botaomasculino);
        botaomasculino.setText("Masculino");
        botaomasculino.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaomasculinoActionPerformed(evt);
            }
        });

        buttonGroupsexo.add(botaofeminino);
        botaofeminino.setText("Feminino");

        labelnascimento.setText("Data de Nascimento");

        txtnascimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtnascimentoActionPerformed(evt);
            }
        });
        txtnascimento.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtnascimentoFocusLost(evt);
            }
        });

        labeladmissao.setText("Data de Admissão");

        txtadmissao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtadmissaoActionPerformed(evt);
            }
        });
        txtadmissao.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtadmissaoFocusLost(evt);
            }
        });

        labelsalario.setText("Salário");

        txtsalario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtsalarioActionPerformed(evt);
            }
        });
        txtsalario.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtsalarioFocusLost(evt);
            }
        });

        labeldesligamento.setText("Data de desligamento");

        txtdesligamento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtdesligamentoActionPerformed(evt);
            }
        });
        txtdesligamento.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtdesligamentoFocusLost(evt);
            }
        });

        botaocadastrar.setText("Cadastrar Empregado");
        botaocadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaocadastrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(65, 65, 65)
                            .addComponent(jLabel1))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(labelnome)
                                .addComponent(labelcpf))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtcpf, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                                .addComponent(txtnome)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(botaomasculino)
                                    .addGap(18, 18, 18)
                                    .addComponent(botaofeminino)))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelsexo)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelnascimento)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtadmissao, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                                    .addComponent(txtnascimento, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                                    .addComponent(txtsalario, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                                    .addComponent(txtdesligamento, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE))))))
                .addGap(76, 76, 76))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labeladmissao)
                .addContainerGap(264, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelsalario)
                .addContainerGap(342, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labeldesligamento)
                .addContainerGap(234, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(153, 153, 153)
                .addComponent(botaocadastrar)
                .addContainerGap(82, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelnome)
                    .addComponent(txtnome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelcpf)
                    .addComponent(txtcpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelsexo)
                    .addComponent(botaomasculino)
                    .addComponent(botaofeminino))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelnascimento)
                    .addComponent(txtnascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labeladmissao)
                    .addComponent(txtadmissao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelsalario)
                    .addComponent(txtsalario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labeldesligamento)
                    .addComponent(txtdesligamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addComponent(botaocadastrar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaomasculinoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaomasculinoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_botaomasculinoActionPerformed

    private void txtadmissaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtadmissaoActionPerformed
        
    }//GEN-LAST:event_txtadmissaoActionPerformed
    private void setNascimento(String nascimento){
        try{
            dtNascimento = (GregorianCalendar) StringParaData.converter(nascimento);
        }catch (Exception ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void setAdmissao(String admissao){
        try{
            dtAdmissao = (GregorianCalendar) StringParaData.converter(admissao);
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this,ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void setDesligamento(String desligamento){
        if (!desligamento.equals("")){
            try{
                dtDesligamento = (GregorianCalendar) StringParaData.converter(desligamento);
            }catch(Exception ex){
                JOptionPane.showMessageDialog(this,ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void setSalario(String sal){
        try{
            salario = Double.parseDouble(sal);
        }catch(Exception ex){
            ex.getMessage();
        }
    }
    
    private void setSexo(){
        if(botaofeminino.isSelected()){
            sexo = "FEMININO";
        }

        if(botaomasculino.isSelected()){
            sexo = "MASCULINO";
        }
    }
    /** Ao pressionar o botão, recebe os valores digitados nas caixas de texto
     * Cria um objeto de Empregado com estes valores fornecidos
     * Cadastra o empregado na arquivo JSON
     */
    private void botaocadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaocadastrarActionPerformed
        cpf = txtcpf.getText();
        nome = txtnome.getText();
        setSexo();
        setNascimento(txtnascimento.getText());
        setAdmissao(txtadmissao.getText());
        setSalario(txtsalario.getText());
        setDesligamento(txtdesligamento.getText());

        Empregado emp = new  Empregado(cpf,nome,sexo,dtNascimento,dtAdmissao,salario,dtDesligamento);
        try{
           ValidateAnnotationParser validar = new ValidateAnnotationParser();
           if(validar.validate(emp)){
               DAOJSON cadastrar = new DAOJSON();
               cadastrar.salvar(emp);
           }
           fechar();
        }catch (Exception ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage(), "Erro ao Cadastrar", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_botaocadastrarActionPerformed

    private void txtnascimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtnascimentoActionPerformed
        
    }//GEN-LAST:event_txtnascimentoActionPerformed

    private void txtdesligamentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtdesligamentoActionPerformed
       
    }//GEN-LAST:event_txtdesligamentoActionPerformed

    private void txtsalarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtsalarioActionPerformed
        
    }//GEN-LAST:event_txtsalarioActionPerformed

    private void txtnomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtnomeActionPerformed

    }//GEN-LAST:event_txtnomeActionPerformed

    private void txtcpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcpfActionPerformed
     
    }//GEN-LAST:event_txtcpfActionPerformed

    private void txtnascimentoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtnascimentoFocusLost

    }//GEN-LAST:event_txtnascimentoFocusLost

    private void txtadmissaoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtadmissaoFocusLost
        
    }//GEN-LAST:event_txtadmissaoFocusLost

    private void txtdesligamentoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtdesligamentoFocusLost
        
    }//GEN-LAST:event_txtdesligamentoFocusLost

    private void txtcpfFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtcpfFocusLost

    }//GEN-LAST:event_txtcpfFocusLost

    private void txtnomeFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtnomeFocusLost

    }//GEN-LAST:event_txtnomeFocusLost

    private void txtsalarioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtsalarioFocusLost
        
    }//GEN-LAST:event_txtsalarioFocusLost

    private void fechar() {
        int response = JOptionPane.showConfirmDialog(this,"Deseja cadastrar outro empregado?","Usuário cadastrado com sucesso", JOptionPane.YES_NO_OPTION);

        if(response == JOptionPane.YES_OPTION){
            txtcpf.setText("");
            txtnome.setText("");
            txtnascimento.setText("");
            txtadmissao.setText("");
            txtsalario.setText("");
            txtdesligamento.setText("");
        }

        if(response == JOptionPane.NO_OPTION){
            this.setVisible(false);
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CadastrarEmpregado dialog = new CadastrarEmpregado(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaocadastrar;
    private javax.swing.JRadioButton botaofeminino;
    private javax.swing.JRadioButton botaomasculino;
    private javax.swing.ButtonGroup buttonGroupsexo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel labeladmissao;
    private javax.swing.JLabel labelcpf;
    private javax.swing.JLabel labeldesligamento;
    private javax.swing.JLabel labelnascimento;
    private javax.swing.JLabel labelnome;
    private javax.swing.JLabel labelsalario;
    private javax.swing.JLabel labelsexo;
    private javax.swing.JTextField txtadmissao;
    private javax.swing.JTextField txtcpf;
    private javax.swing.JTextField txtdesligamento;
    private javax.swing.JTextField txtnascimento;
    private javax.swing.JTextField txtnome;
    private javax.swing.JTextField txtsalario;
    // End of variables declaration//GEN-END:variables

}
